﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SnmpForm.DbWrapper.DbDataTypes;

namespace SnmpForm.ApSearch
{
    public partial class ucApSerachConf : UserControl
    {
        public ucApSerachConf()
        {
            InitializeComponent();

            //屏蔽修改菜单
            ToolStripMenuItemModify.Visible = false;
        }

        public virtual bool AddEvent(DbWrapper.DbDataTypes.SearchTarget target)
        {
            return BusinessWrapper.User.Instance.AddSearchTarget(target);
        }

        public virtual bool DelEvent(DbWrapper.DbDataTypes.SearchTarget target)
        {
            return BusinessWrapper.User.Instance.DelSearchTarget(target); 
        }
        public virtual List<DbWrapper.DbDataTypes.SearchTarget>
            GetEvent()
        {
            return BusinessWrapper.User.Instance.GetSearchTargetList();
        }
        public virtual bool ModifyEvent(DbWrapper.DbDataTypes.SearchTarget target)
        {
            return true;
        }
        private void buttonAdd_Click(object sender, EventArgs e)
        {
            SearchTarget target = ucSearchTarget1.Value;
            System.Net.IPAddress addrFrom = 
                System.Net.IPAddress.Parse(target.from);
            System.Net.IPAddress addrTo = 
                System.Net.IPAddress.Parse(target.to);

            if (IPAddressExtend.LessOrEquals(addrFrom, addrTo))
            {
                if (!addrTo.Equals(IPAddressExtend.ZeroAddress))
                {
                    bool bSuccess = AddEvent(target);
                    if (bSuccess)
                    {
                        MessageBox.Show("添加成功");
                        UpdateSearchTargetView();
                    }
                    else
                    {
                        MessageBox.Show("添加失败");
                    }
                }
                else
                {
                    MessageBox.Show("结束ip不可以为0");
                }
            }
            else
            {
                MessageBox.Show("开始位置必须小于结束位置");
            }
        }

        private void ToolStripMenuItemDel_Click(object sender, EventArgs e)
        {
            DbWrapper.DbDataTypes.SearchTarget target =
                GetSelectedTarget();
            bool bSuccess =
                DelEvent(target);
            if (bSuccess)
            {
                MessageBox.Show("删除成功");
                UpdateSearchTargetView();
            }
            else
            {
                MessageBox.Show("删除失败");
            }
        }

        private void contextMenuStripSearchTarget_Opening(object sender, CancelEventArgs e)
        {
            if (GetSelectedTarget() == null)
            {
                e.Cancel = true;
            }
        }

        private void ucApSerachConf_Load(object sender, EventArgs e)
        {
            //listView
            listViewTarget.View = View.Details;
            listViewTarget.FullRowSelect = true;
            listViewTarget.Columns.Add("编号");
            listViewTarget.Columns.Add("起始IP", 166);
            listViewTarget.Columns.Add("结束IP", 166);
            
            UpdateSearchTargetView();
        }


        private void UpdateSearchTargetView()
        {
            listViewTarget.Items.Clear();
            try
            {
                List<DbWrapper.DbDataTypes.SearchTarget> listTarget =
                    GetEvent();
                foreach (DbWrapper.DbDataTypes.SearchTarget target in listTarget)
                {
                    ListViewItem lvi = new ListViewItem();
                    lvi.Text = target.id.ToString();
                    lvi.SubItems.Add(target.from);
                    lvi.SubItems.Add(target.to);
                    listViewTarget.Items.Add(lvi);
                }
            }
            catch
            {
            }
        }


        private DbWrapper.DbDataTypes.SearchTarget GetSelectedTarget()
        {
            DbWrapper.DbDataTypes.SearchTarget target = null;

            if (listViewTarget.SelectedItems.Count > 0)
            {
                target = new DbWrapper.DbDataTypes.SearchTarget();
                ListViewItem item = listViewTarget.SelectedItems[0];
                target.id = Convert.ToInt32(item.Text);
            }
            return target;
        }

        private void ToolStripMenuItemModify_Click(object sender, EventArgs e)
        {
            DbWrapper.DbDataTypes.SearchTarget target =
                GetSelectedTarget();
        }
    }
}
